package com.example.admin.testdemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.admin.testdemo.R;
import com.example.admin.testdemo.rs232.Command;
import com.example.admin.testdemo.rs232.Serial;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SerialConnectActivity extends BaseActivity implements View.OnClickListener {
    private static String TAG = "SerialConnectActivity";
    private Button btn_485;
    private boolean engineQueryFlag = false;
    private boolean chekQueryFlag = false;
    private boolean looperTest = false;
    private Serial serial;
    private ScheduledExecutorService scheduledThreadPool;
    private int flagMonitor = 0;
    private Serial.DataCallback callback = new Serial.DataCallback() {
        @Override
        public void onDataReceived(byte[] buffer, int size) {
            Log.e(TAG, "接收：" + Command.printByteInfo(buffer));
            if(looperTest){
                handMonitorResult(buffer);
                handleCheck(buffer);
            }
//            singleDebug(buffer);
//            handhandshae(buffer);
//            handcheck(buffer);
//            handleConnect(buffer);
//            handMonitor(buffer);

        }
    };

    private void handleCheck(byte[] buffer) {
//        if (buffer[0] == (byte) 0xAA&&buffer[2] == (byte) 0xF5&&buffer[5] == (byte) 0x30) {
//            engineQueryFlag=false;
//            chekQueryFlag=true;
//        }
    }

    private void handMonitorResult(byte[] buffer) {
        if (buffer[0] == (byte) 0xAA&&buffer[2] == (byte) 0xF5&&buffer[5] == (byte) 0x30) {
            engineQueryFlag=false;
            chekQueryFlag=true;
        }else if(buffer[0] == (byte) 0xAA&&buffer[2] == (byte) 0xF5&&buffer[5] == (byte) 0x31){
            engineQueryFlag=true;
            chekQueryFlag=false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serial_connect);
        serial = Serial.getSerialInstance();
        serial.setCallback(callback);
        initView();
        initData();
        initTimer();
    }

    private void initTimer() {
        if (scheduledThreadPool == null) {
            scheduledThreadPool = new ScheduledThreadPoolExecutor(1);
        }
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (looperTest) {
                    flagMonitor = 0;
                    engineQueryFlag=false;
                    chekQueryFlag=false;
                    setEngineCmd();
                }
            }
        }, 0, 10000, TimeUnit.MILLISECONDS);
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (looperTest) {
                    if (flagMonitor > 0 && flagMonitor < 4) {
                        serial.sendData(Command.engineResult(1));
                    }else if (flagMonitor > 4 && flagMonitor < 10) {
                        serial.sendData(Command.outGoodsResult());
                    }
                    flagMonitor++;
                }
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);

    }


    private void setEngineCmd() {
        if (serial != null) {
            engineQueryFlag=true;
            serial.sendData(Command.outGoods(1, 1, 1));
        }
    }

    private void initData() {
        btn_485 = findViewById(R.id.btn_485);
        btn_485.setOnClickListener(this);
    }

    private void setBoolean2F() {
        engineQueryFlag = false;
        chekQueryFlag = false;
    }

    private void initView() {
    }


    private boolean isBtn485 = true;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_485:
                btn485();
                break;
        }
    }

    private void btn485() {
        if (isBtn485) {
            isBtn485 = false;
            looperTest = true;
            btn_485.setText("测试中");
        } else {
            looperTest = false;
            btn_485.setText("测试");
            isBtn485 = true;
        }
    }

    private void setEt(final EditText et, final String flag) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                et.setText(flag);
            }
        });
    }
}
